package com.yantong.utils;


import org.apache.commons.lang3.StringUtils;

import java.net.UnknownHostException;
import java.util.Date;
import java.util.Random;

/**
 * @author
 */
public class CommonUtils {

    public static boolean isCarNo(String carNo){
        if (StringUtils.isEmpty(carNo)) {
            return false;
        }
        String reg = "^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(?:(?![A-Z]{4})[A-Z0-9]){4}[A-Z0-9挂学警港澳]{1}$";
        if (!carNo.matches(reg)){
            return false;
        }
        return true;
    }


    /**
     * 获取随机码
     * @param len
     * @return
     */
    public static String generateRandomStr(int len) {
        //字符源，可以根据需要删减
        String generateSource = "23456789abcdefghgklmnpqrstuvwxyz";//去掉1和i ，0和o
        return randomStr(generateSource,len);
    }

    public static String generateNumberRandomStr(int len) {
        //字符源，可以根据需要删减
        String generateSource = "1234567890";
        return randomStr(generateSource,len);
    }

    public static String getRandomString(int len) { //length表示生成字符串的长度
        String generateSource = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        return randomStr(generateSource,len);
    }

    private static String randomStr(String generateSource , int len){
        String rtnStr = "";
        for (int i = 0; i < len; i++) {
            //循环随机获得当次字符，并移走选出的字符
            String nowStr = String.valueOf(generateSource.charAt((int) Math.floor(Math.random() * generateSource.length())));
            rtnStr += nowStr;
            generateSource = generateSource.replaceAll(nowStr, "");
        }
        return rtnStr;
    }


    /**
     * 获取序列化
     * @return
     */
    public static String getSn(String prefix) {
        Random random = new Random();
        String nx = DateUtils.format(new Date(), "yyyyMMddHHmmssSSS");
        return  prefix.concat(nx).concat(String.valueOf(random.nextInt(1000)));
    }


    public static void main(String[] args) throws UnknownHostException {
//        System.out.println(isCarNo("粤SUP985"));
//        System.out.println(InetAddress.getLocalHost().getHostAddress());
    }
}
